/**
 * 
 * 可编辑表格上的悬浮按钮
 */
$(function() {
	var $orderButton = $(".formdata td").children(".order-no");
	$orderButton.click(function() {
		($(this).next(".order-no-search")).show();
//		search(this);
		var index = $(this).parent().parent().parent().find("tr").index($(this).parent().parent()[0]);
		$(".formdata td").children(".order-no").each(function() {
			var newIndex = $(this).parent().parent().parent().find("tr").index($(this).parent().parent()[0]);
			if (newIndex != index) {
				($(this).next(".order-no-search")).hide()
			}
		});
	});
	$orderButton.next(".order-no-search").click(function() {
		openGoodsWin($(this).parent().prev().children("span").text())
	});
	($(".formdata td").filter(function() {
		return $(".order-no", this).length == 0;
	})).click(function() {
		($(".formdata td").children(".order-no").next(".order-no-search")).hide()
	});
	$(".clear-btn").click(function() {
		clearRow(this);
		payMent();
	});
	$("#new").click(function() {
		newRow()
	});
	$("#clear").click(function() {
		clearAll()
	});
	$(".quantity").blur(function() {
		totalSum($(this).parent().parent());
	});
	$(".discount").blur(function() {
		totalSum($(this).parent().parent());
	});
	$(".unitPrice").blur(function() {
		totalSum($(this).parent().parent());
	});
	$("#coupon").click(function() {
		coupon()
	});
	$("#postage").blur(function() {
		postAge()
	});
});

/**
 * 新增一行
 */
function newRow() {
	var listNo = $(".formdata tr:last-child td:first-child").children("span").text();
	listNo = parseInt(listNo) + 1;
	var $newCol = $(".formdata tr:last-child").clone(true);
	defaultText($newCol);
	$(".formdata").append($newCol);
	$(".formdata tr:last-child td:first-child").children("span").text(listNo)
}

/**
 * 清除行列内容
 */
function clearRow(obj) {
	var $rows = $(obj).parent().parent();
	if ($rows.children(".order-no-td").children("input:first-child").val() != "") {
		defaultText($rows)
	} else if (($(".formdata").find("tr").length - 1) > 1) {
		orderNo = $rows.children("td:first-child").children("span").text();
		resetOrder(orderNo);
		$rows.remove()
	}
}

/**
 * 清空所有行列数据
 */
function clearAll() {
	$rowsAll = $(".formdata tr:eq(0)").nextAll("tr");
	$rowsAll.each(function() {
		var orderNo = $(this).children("td:first-child").children("span").text();
		if (orderNo == 1) {
			defaultText($(this))
		} else {
			$(this).remove()
		}
	});
}
	
/**
 * 重置行列内容为空
 */
function defaultText($row) {
	var $inputs = $row.find("input[type='text']");
	var $hiddens = $row.find("input[type='hidden']");
	var $divs = $row.find(".goods-item");
	$inputs.each(function() {
		$(this).val("")
	});
	$hiddens.each(function() {
		$(this).val("")
	});
	$divs.each(function() {
		$(this).text("")
	});
}
	
/**
 * 重置序号
 */
function resetOrder(thisCol) {
	var $nextAll = $(".formdata tr:eq(" + (thisCol - 1) + ")").nextAll("tr");
	$nextAll.each(function() {
		var orderNoIndex = $(this).children("td:eq(0)").children("span").text();
		$(this).children("td:eq(0)").children("span").text(orderNoIndex - 1)
	});
}

/**
 * 快捷搜索
 */
function search(obj) {
	$(obj).bind("keyup", function() {
		if (($(obj).val().length > 4)) {
			if (! $(obj).next().is("div")) {
				$(obj).after("<div class='search-list'></div>");
			}
		} else if ($(obj).next().is("div")) {
			$(obj).next().remove();
		}
	});
}

/**
 * 删除无数据的行列
 */
function clearNullData() {
	$rowsAll = $(".formdata tr:eq(0)").nextAll("tr");
	$rowsAll.each(function() {
		var orderNo = $(this).children("td:first-child").children("span").text();
		var goodsNo = $(this).children("td:eq(1)").children("input:first-child").val();
		if ((($(".formdata").find("tr").length - 1) > 1) && (goodsNo == "")) {
			resetOrder(orderNo);
			$(this).remove();
		}
		var quantitys = $(this).children("td").children(".quantity").val();
		var stock = $(this).children("td").children(".stock").text();
		if ((quantitys != "") && (stock !="")) {
			quantitys = parseInt(quantitys);
			stock = parseInt(stock);
			stock -= quantitys;
			$(this).children("td").children(".stock").prev().val(stock);
			$(this).children("td").children(".stock").text(stock);
		}
	});
}

/**
 * 单价 * 数量 * 折扣
 */
function totalSum($obj) {
	var quantity = $obj.find(".quantity").val();
	var discount = $obj.find(".discount").val();
	var unitPrice = $obj.find(".unitPrice").val();
	if ((quantity != "") && (discount != "") && (unitPrice != "")) {
		discount = parseFloat(discount);
		quantity = parseInt(quantity);
		unitPrice = parseFloat(unitPrice);
		var sum = unitPrice * quantity * discount;
		$obj.find(".sum").text(sum);
		payMent()
	}
}

/**
 * 货款总额、抵押积分合计
 */
function payMent() {
	var total = 0;
	var payMent = 0;
	var creditNum = 0;
	$rowsAll = $(".formdata tr:eq(0)").nextAll("tr");
	$rowsAll.each(function() {
		if ($(this).children("td:eq(1)").children("input:first-child").val() != "") {
			var sum = $(this).find(".sum").text();
			var credit = $(this).find(".credit").val();
			sum = parseFloat(sum);
			credit = parseInt(credit);
			total += sum;
			creditNum += credit;
		}
	});
	$("#total").text(total + "元");
	$("#total").next("input").val(total + "元");
	var postAge = $("#postage").val();
	if (postAge == "") {
		postAge = 0;
	}
	postAge = parseFloat(postAge);
	payMent = total + postAge;
	$("#payment").text(payMent + "元");
	$("#creditNum").text(creditNum);
	$("#creditNum").next("input").val(creditNum);
}

/**
 * 邮资
 */
function postAge() {
	var postAge = $("#postage").val();
	if (postAge == "") {
		postAge = 0;
	}
	postAge = parseFloat(postAge);
	var total = $("#total").text();
	if (total == "") {
		total = 0;
	}
	total = parseFloat(total);
	var payMent = total + postAge;
	$("#payment").text(payMent + "元");
}
	
/**
 * 使用优惠券
 */
function coupon() {
	var customerId = $("#customerId").val();
	var couponNo = $("#coupon").prev().val();
	$.post(base + "admin/coupon/checkCoupon.action", 
		{
			"coupon.couponNo" : couponNo, 
			"coupon.customerId": customerId
		}, function(data) {
			data = eval("(" + data + ")");
			if (data['status'] == "error") {
				alert(data['result']);
			} else {
				var payMent = $("#payment").text();
				payMent = parseFloat(payMent);
				payMent = payMent - data['result'];
				$("#payment").text(payMent + "元");
				$("#payment").next("input").val(payMent + "元");
				$("#coupon").val("已使用").attr("disabled", true);
			}
	});
}
	
/**
 * 查询窗口，需dialog js库
 */
function openGoodsWin(orderNo){
	var diag = new top.Dialog();
	diag.Title = "查询商品";
	diag.URL = base + "admin/goods/listByPage.action?orderNo=" + orderNo;
	diag.Width="100";
	diag.Height="100";
	diag.CancelEvent = function(){
		diag.innerFrame.contentWindow.location.reload();
		diag.close();
	};
	diag.show();
}
